package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author dongdongxiaoyuanmao
 * @since 2025-03-06
 */
@RestController
@RequestMapping("/productUnit")
@Tag(name = "商品单位接口管理")
public class ProductUnitController extends BaseController {

    @Autowired
    private IProductUnitService productUnitService;

    @Operation(summary = "查询单位列表")
    @GetMapping("/list")
    public TableDataInfo list(String name) {
        startPage();
        List<ProductUnit> list = productUnitService.selectProductUnitList(name);
        //封装分页列表
        return getDataTable(list);
    }

    @Operation(summary = "根据id获取单位详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        ProductUnit productUnit = productUnitService.getById(id);
        return success(productUnit);
    }

    @Operation(summary = "新增单位")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ProductUnit productUnit) {
        productUnit.setCreateBy(SecurityUtils.getUsername());
        boolean result = productUnitService.save(productUnit);
        return toAjax(result);
    }

    @Operation(summary = "修改单位")
    @PutMapping
    public AjaxResult edit(@RequestBody ProductUnit productUnit) {
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateById(productUnit));
    }

    @Operation(summary = "删除单位")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productUnitService.removeBatchByIds(ids));
    }

    @Operation(summary = "获取所有单位")
    @GetMapping("/getUnitAll")
    public AjaxResult getUnitAll() {
        return success(productUnitService.list());
    }

}
